$def with (get_results)

$ q = query_param('q')
$ results_per_page = 100
$ page = query_param('page')
$if page:
    $ page = int(page)
$else:
    $ page = 1
$ offset = (page - 1) * results_per_page
$ url_map = { 'person': 'person:', 'place': 'place:', 'time': 'time:' }

<div id="contentHead">
    <h1>
        $_("Subject Search")
    </h1>

$if q:
    $ results = get_results(q, offset=offset, limit=results_per_page)
    $if 'error' not in results:
        $ response = results['response']
        $ num_found = int(response['numFound'])
        <p class="sansserif darkgreen collapse"><strong>$ungettext('1 hit', '%(count)s hits', response['numFound'], count=commify(response['numFound']))</strong></p>
</div>

<div id="contentBody">
    <div class="section">
        <form class="siteSearch olform" action="">
            <input type="text" class="larger" name="q" size="100" value="$q"/>
            <input type="submit" class="large" value="$_('Search')"/>
        </form>
    </div>

$if q and 'error' in results:
    <strong>
        $for line in results['error'].splitlines():
            $line
            $if not loop.last:
                <br>
    </strong>

$if q and 'error' not in results:
    <ul class="subjectList">
    $for doc in response['docs']:
        $ n = doc['name']
        $ key = '/subjects/' + url_map.get(doc['type'], '') + n.lower().replace(' ', '_').replace('?', '').replace(',', '')

        <li>
            <a href="$key">$n</a>
            $code:
                def find_type():
                    if doc['type'] == 'time':
                        return "type_time"
                    elif doc['type'] == 'subject':
                        return "type_subject"
                    elif doc['type'] == 'place':
                        return "type_place"
                    elif doc['type'] == 'org':
                        return "type_org"
                    elif doc['type'] == 'event':
                        return "type_event"
                    elif doc['type'] == 'person':
                        return "type_person"
                    elif doc['type'] == 'work':
                        return "type_work"
                    else:
                        return "other"
                type = find_type()
                if type == "type_time":
                    note = '<span class="teal">' + _("time") + '</span>'
                elif type == "type_subject":
                    note = '<span class="darkgreen">' + _("subject") + '</span>'
                elif type == "type_place":
                    note = '<span class="orange">' + _("place") + '</span>'
                elif type == "type_org":
                    note = '<span class="blue">' + _("org") + '</span>'
                elif type == "type_event":
                    note = '<span class="grey">' + _("event") + '</span>'
                elif type == "type_person":
                    note = '<span class="red">' + _("person") + '</span>'
                elif type == "type_work":
                    note = '<span class="black">' + _("work") + '</span>'
                else:
                    note = doc['type']
            <span class="count">&nbsp;&nbsp;<b>$ungettext('1 book', '%(count)d books', doc['count'], count=doc['count'])</b>, $:note</span>
        </li>
    </ul>
    $:macros.Pager(page, num_found, results_per_page)

</div>
